#! /bin/bash

checkError() {
    error=$?

    if [ $error -ne 0 ]; then
        echo "found error: $error"
        exit $error
    fi
}

program_name=my-golang-app
product_installer_home="/root"

echo "unzip the packager ${product_installer_home}/${program_name} ..."
pushd ${product_installer_home}
tar -xvf ./${program_name}.tar.bz2
chmod -R 777 ./dist


echo "try to kill the current instance ..."
ps -aux | grep mygolang
app_pid=$(ps -ef | grep -wv grep | grep ${program_name} | tr -s ' ' | cut -d ' ' -f2)
echo pid: $app_pid
if [[ ! -z "$app_pid" ]]; then
    echo kill $app_pid
    kill $app_pid
fi

echo "start ..."
nohup ./dist/${program_name} > ./dist/1.log 2>&1 &
popd